package copyengine.utils.delayEffect.effect.extend
{
	import copyengine.utils.GeneralUtils;
	import copyengine.utils.cache.object.ICEMovieClip;
	import copyengine.utils.delayEffect.effect.basic.CEDelayEffectBasic;
	import copyengine.utils.tick.GlobalTick;

	import flash.display.DisplayObject;
	import flash.display.MovieClip;

	public class CEMovieClipDelayEffect extends CEDelayEffectBasic
	{
		public static const PLAY_TYPE_PLAY_AND_REMOVE:int = 1;
		public static const PLAY_TYPE_PLAY_AND_STOP:int = 2;

		private var targetMc:DisplayObject;
		private var targetMcPlayType:int;
		private var layerType:int;
		private var posX:int;
		private var posY:int;

		private var flowFinishFrame:int;
		private var isRemoveEffectOnFlowFinish:Boolean;

		public function CEMovieClipDelayEffect()
		{
			super();
		}

		/**
		 *
		 * @param _targetMc												需要播放动画的Mc
		 * @param _layerType												#see CEDelayEffectBasic
		 * @param _posX														坐标X    (根据_layerType来对应是哪个坐标系)
		 * @param _posY														坐标Y    (根据_layerType来对应是哪个坐标系)
		 * @param _targetMcPlayType								PLAY_TYPE_PLAY_AND_REMOVE || PLAY_TYPE_PLAY_AND_STOP
		 * @param _flowFinishFrame									默认会在TargetMc全部播放完成后才认为Effect播放结束,但是有些时候会需要
		 * 播放结束时间早于Mc结束的时间,此时指定该参数,指定结束帧
		 * @param _isRemoveEffectOnFlowFinish				是否在整个组合Effect的Flow完成后删除当前Target,默认为true
		 * 如果为false则会在整个Effect被dispose时候才被删除
		 *
		 */
		public function initialize(_targetMc:DisplayObject,
								   _layerType:int,
								   _posX:int, _posY:int,
								   _targetMcPlayType:int,
								   _flowFinishFrame:int = int.MIN_VALUE,
								   _isRemoveEffectOnFlowFinish:Boolean = true):void
		{
			targetMc = _targetMc;
			layerType = _layerType;
			posX = _posX;
			posY = _posY;
			targetMcPlayType = _targetMcPlayType;
			flowFinishFrame = _flowFinishFrame;
			isRemoveEffectOnFlowFinish = _isRemoveEffectOnFlowFinish;
		}

		override protected function doExecute():void
		{
			//先将Mc停留在第一帧，因为加入Mc的当帧不会播放动画，而是在下一帧开始播放
			//并且Movieclip是在创建时候就会默认开始播放，因为播放时候还没有播放到当前Effect,
			//所以如果此时将播放一半的mc加入舞台，并且在下一帧才PlayMovieClip，就会有一帧的闪动
			if (targetMc is MovieClip)
			{
				(targetMc as MovieClip).gotoAndStop(1);
			}
			else if (targetMc is ICEMovieClip)
			{
				(targetMc as ICEMovieClip).gotoAndStop(1);
			}

			getLayerByType(layerType).addChild(targetMc);
			targetMc.x = posX;
			targetMc.y = posY;

			if (targetMcPlayType == PLAY_TYPE_PLAY_AND_REMOVE)
			{
				GlobalTick.instance.playAndRemoveMovieClip(targetMc, targetMcOnPlayFinish);
			}
			else
			{
				GlobalTick.instance.playAndStopMovieClip(targetMc, targetMcOnPlayFinish);
			}
			if (flowFinishFrame != int.MIN_VALUE)
			{
				GlobalTick.instance.callLaterAfterTickCount(onMiddleCallFlowFinish, flowFinishFrame);
			}
		}

		override protected function doDispose():void
		{
			GeneralUtils.removeTargetFromParent(targetMc);
			GlobalTick.instance.removeTickNodeByFunction(targetMcOnPlayFinish);
			GlobalTick.instance.removeTickNodeByFunction(onMiddleCallFlowFinish);
			targetMc = null;
		}

		override public function onFlowExecuteFinish():void
		{
			isRemoveEffectOnFlowFinish && GeneralUtils.removeTargetFromParent(targetMc);
		}

		/**
		 *在指定了flowFinishFrame的情况下会在播放途中回调该函数
		 */
		private function onMiddleCallFlowFinish():void
		{
			callToFinishFlow();
			callToFinishExecute();
		}

		/**
		 * 在当前Mc播放完成一遍后回调
		 */
		private function targetMcOnPlayFinish():void
		{
			callToFinishFlow();
			callToFinishExecute();
		}
	}
}
